import { getMenuData } from '@/services/menu';
import { Effect } from 'dva';
import { Reducer } from 'redux';

import { queryList, queryDetail } from '@/services/connectPlat';

export interface ConnectPlatReportLogListItem {
  id?: string; // 列表id
  vin?: string; // vin
  reportType?: string; // 上报类型 1-OMS提醒2-告警提醒3-vin码冲突4-诊断上报5-can网络异常
  protocolVersion?: string; //协议版本号
  messageId?: string; //消息id
  serviceType?: string; //服务类型
  messageType?: string; //消息类型
  messageSendTime?: string; //消息发送时间
  messageVersion?: string; //消息版本号
  dataLength?: number; //数据长度
  [key: string]: any;
}
export interface ConnectPlatReportLogDetail {
  reportType?: string; // 上报类型 1-OMS提醒2-告警提醒3-vin码冲突4-诊断上报5-can网络异常
  reportDetail?: Object; //上报详情json
  [key: string]: any;
}
/**
 * 标签项
 */
export interface LabelItem {
  label?: string;
  value?: string;
  [key: string]: any;
}
export interface ConnectPlatModelState {
  connectReportLog?: {
    logList: Array<ConnectPlatReportLogListItem>;
    pagination: { pageSize: number; current: number };
  };
  connectReportLogDetail: ConnectPlatReportLogDetail;
  [key: string]: any;
}

export interface ConnectPlatModelType {
  namespace: 'connectPlat';
  state: ConnectPlatModelState;
  effects: {
    queryList: Effect;
    queryDetail: Effect;
  };
  reducers: {
    save: Reducer<ConnectPlatModelState>;
  };
}

const ConnectPlatModel: ConnectPlatModelType = {
  namespace: 'connectPlat',
  state: {
    connectReportLog: {
      logList: [],
      pagination: {
        pageSize: 10,
        current: 1,
      },
    }, // 上报日志列表
    connectReportLogDetail: {},
    response: {},
  },

  effects: {
    /**
     * 上报日志查询
     * @param _
     * @param param
     */
    *queryList({ payload, callback }, { call, put }) {
      const response = yield call(queryList, payload);
      if (response.statusCode === '0') {
        const { itemSize = 0, pageNo = 1, pageSize = 10 } = response.data;
        const pageData = {
          current: pageNo,
          pageSize,
          total: itemSize,
        };
        yield put({
          type: 'save',
          payload: {
            connectReportLog: { logList: response.data?.list, pagination: pageData },
          },
        });
        if (callback) callback(response.data);
      }
    },
    /**
     * 上报日志详情
     * @param _
     * @param param
     */
    *queryDetail({ payload, callback }, { call, put }) {
      const response = yield call(queryDetail, payload);
      if (response.statusCode === '0') {
        yield put({
          type: 'save',
          payload: {
            connectReportLogDetail: response.data,
          },
        });
        if (callback) callback(response.data);
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default ConnectPlatModel;
